<?php

class StController extends AdminController {

    public $layout = 'opd_content';

    protected function getActionsKey() {
        return __CLASS__;
    }

    public function init() {
        parent::init();
//		$this->controllerLable = 'Cấu hình website';
    }

    public function actionAd() {
        $model = new Settings('search');
        $model->unsetAttributes();
        if (isset($_GET['Settings']))
            $model->attributes = $_GET['Settings'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionAdmin() {
//		$this->setUnionTitle('Cấu hình website');
        $model = new Settings('search');
        $model->unsetAttributes();
        if (isset($_GET['Settings']))
            $model->attributes = $_GET['Settings'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {

        $model = new OpdSettings();


        if (isset($_POST['OpdSettings'])) {
            $model->attributes = $_POST['OpdSettings'];

            if ($model->save()) {
                echo 'thanh cong';
            } else {
                echo CActiveForm::validate($model);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate() {
        if (isset($_POST['OpdSettings'])) {
            $error[] = array();
            foreach ($_POST['OpdSettings'] as $key => $value) {
                $model = OpdSettings::model()->find('`key`="' . $key . '"');
                $model->value = serialize($value);
                if ($model->update()) {
//                    echo 'ok';
                } else {
                    $error[] = $key;
                }
            }
        }
        if (isset($_POST['Support'])) {
            $arr = array();
            foreach ($_POST['Support'] as $k => $v) {
                $arr[]=array(
                    'uid'=>$v['uid'],
                    'phone'=>$v['phone'],
                    'type'=>$v['type']
                );
            }
            $model=  OpdSettings::model()->findByAttributes(array('key'=>'support_online'));
            $model->value=  serialize($arr);
            $model->update();
        }
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $model = $this->loadModel($id);
            $this->attachLogBehavior($model);
            $model->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function loadModel($id) {
        $model = Settings::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'settings-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
